/*
 * @Descripttion:过滤器方法抽离
 * @version:
 * @Author: zhangfan
 * @email: 2207044692@qq.com
 * @Date: 2020-08-28 15:43:57
 * @LastEditors: younghxp
 * @LastEditTime: 2022-03-09 09:54:22
 */

/**
 * @description: 数据装换
 */
const numFormat = function (num) {
  let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  let unit = ['', '十', '百', '千', '万']
  num = parseInt(num)
  let getWan = (temp) => {
    let strArr = temp.toString().split('').reverse()
    let newNum = ''
    for (var i = 0; i < strArr.length; i++) {
      newNum =
        (i == 0 && strArr[i] == 0
          ? ''
          : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0
          ? ''
          : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) +
        newNum
    }
    return newNum
  }
  let overWan = Math.floor(num / 10000)
  let noWan = num % 10000
  if (noWan.toString().length < 4) {
    noWan = '0' + noWan
  }
  let numText = overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
  if (num > 9 && num < 20) {
    numText = numText.substring(1, 3)
  } else if (num >= 100000 && num < 200000) {
    numText = numText.substring(1, 5)
  }

  return numText
}

/**
 * @description: 数据脱敏
 */
const deseHandler = function (input, frontLen, endLen, wildcard, type) {
  if (!input) {
    return '--'
  } else if (input.length < 9) {
    return input
  } else {
    var len = 4
    var xing = ''
    for (var i = 0; i < len; i++) {
      xing += wildcard
    }
    if (type == 'mark') {
      return input.substring(0, input.length - 4) + xing
    } else {
      return (
        input.substring(0, frontLen) +
        xing +
        input.substring(input.length - endLen)
      )
    }
  }
}
// 手机号脱敏
const telHandler = function (val) {
  if (!val) {
    return '--'
  } else {
    var xing = '****'
    return (
      val.substring(0, 3) + xing + val.substring(val.length - 4, val.length)
    )
  }
}
// 数字逢三一断
const splitNumber = function (value) {
  if (value === null || value === undefined) return '--'
  if (value === 0 || value === '0') return '0'
  // if (!value) return "0.00";
  var intPart = parseInt(Number(value)) //获取整数部分
  var floatPart = ''
  if ((value + '').indexOf('.') > -1) {
    floatPart = '.' + (value + '').split('.')[1]
  }
  var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') //将整数部分逢三一断
  if (intPart === 0 && 1 / intPart < 0) {
    return '-' + intPartFormat + floatPart
  } else {
    return intPartFormat + floatPart
  }

  // return intPartFormat;
  // var floatPart = ".00"; //预定义小数部分
  // var value2Array = value.split(".");
  // //=2表示数据有小数位
  // if (value2Array.length == 2) {
  //     floatPart = value2Array[1].toString(); //拿到小数部分
  //     if (floatPart.length == 1) {
  //         //补0,实际上用不着
  //         return intPartFormat + "." + floatPart + "0";
  //     } else {
  //         return intPartFormat + "." + floatPart;
  //     }
  // } else {
  //     return intPartFormat + floatPart;
  // }
}
/**
 * @description: 小于10补0
 * @param {*} i
 * @return {*}
 */
function checkTime(i) {
  if (i < 10) {
    i = '0' + i
  }
  return i
}
/**
 * @description: 2021-11-01T16:02:32.000+00:00 格式化 yy-mm-dd
 * @param {*} date
 * @return {*}
 */
const dateHandle = function (date) {
  if (date) {
    let newDate = new Date(date)
    const y = checkTime(newDate.getFullYear().toString())
    const m = checkTime((newDate.getMonth() + 1).toString())
    const d = checkTime(newDate.getDate().toString())
    return y + '-' + m + '-' + d
  }
  return '--'
}
/**
 * @description: 2021-11-01T16:02:32.000+00:00 格式化 yy-mm-dd hh:mm:ss
 * @param {*} time
 * @return {*}
 */
const timeHandle = function (time) {
  if (time) {
    let date = new Date(time)
    const y = checkTime(date.getFullYear().toString())
    const m = checkTime((date.getMonth() + 1).toString())
    const d = checkTime(date.getDate().toString())
    const hours = checkTime(date.getHours().toString())
    const minutes = checkTime(date.getMinutes().toString())
    const seconds = checkTime(date.getSeconds().toString())
    return y + '-' + m + '-' + d + ' ' + hours + ':' + minutes + ':' + seconds
  }
  return '--'
}
export default {
  numFormat,
  deseHandler,
  telHandler,
  splitNumber,
  dateHandle,
  timeHandle,
}
